<template>
    <!-- 新增和修改 -->
    <div class="modal fade bs-example-modal-lg" id="addhealthnews" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title" id="myModalLabel">
                {{modaltitle}}</h4>
                </div>
                <div class="fail cbafter" v-show="healthnewssavedata.verifyState=='2'">
                    <div class="textdes">
                        <p class="nosuccess">审核未通过！</p>
                        <p>{{healthnewssavedata.verifySuggestion}}</p>
                    </div>
                </div>
                <div class="changeinfo cbafter">
                    <div class="changeinfo_head cbafter">
                        <span>申请人：{{username}}</span>
                        <span>申请时间：{{applytime}}</span>
                    </div>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form" id="changedocform" v-on:submit.prevent="addhealthnewssave">
                        <div class="form-group">
                            <label class="col-md-3 control-label">资讯标题 :</label>
                            <div class="col-md-8">
                                <input type="text" class="form-control" v-model="healthnewssavedata.infoTitle" required />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-3 control-label">标题图片 :</label>
                            <div class="col-md-8">
                                <img-upload-btn :imgurl="imgurl"></img-upload-btn>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-3 control-label">资讯类别 :</label>
                            <div class="col-md-8">
                                <select class="form-control" v-model="healthnewssavedata.infoType" required>
                                    <option v-for="item in newstypelist" value="{{item.key}}">{{item.text}}</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-3 control-label">发布内容 :</label>
                            <div class="col-md-8">
                                <div id="healthnews" class="summernote"></div>
                                <!--  <textarea class="form-control" rows="3" placeholder="请输入讲座内容" v-model="healthnewssavedata.infoContent" required></textarea> -->
                            </div>
                        </div>
                        <div class="form-group save">
                            <button type="submit" class="default ok center">确认</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import Public from 'assets/public.js'
import ImgUploadBtn from 'components/ImgUploadBtn.vue'
import 'assets/lib/bootstrap-summernote/summernote.css'
require('summernote')
module.exports = {
    data() {
            return {
                healthnewssavedata: {
                    "infoTitlePicture": 0,
                    "infoTitle": "",
                    "infoContent": "",
                    "infoType": "",
                },
                newstypelist: [], //资讯类别列表
            }
        },
        components: {
            ImgUploadBtn
        },
        props: {
            "username": {
                type: String,
                default: ""
            },
            "applytime": {
                type: String,
                default: ""
            },
            "imgurl": {
                type: String,
                default: ""
            },

            "modaltitle": {
                type: String,
                default: ""
            },
             "imgsize": {
                type: Number,
                default: 102400
            },

        },
        ready() {
            var that = this;
            // 获取医生职称字典
            $.when(Public.commonajax("coms.multipleDictionaryService", "findDic", "[['base.dictionary.base_informationType']]"))
                .done(function(res) {
                    var data = res.body;
                    that.newstypelist = data[0].items

                });
            // 资讯内容编辑
            $('#healthnews').summernote({
                callbacks: {
                    onImageUpload: function(files) {
                        //上传图片到服务器，使用了formData对象，至于兼容性...据说对低版本IE不太友好
                        var formData = new FormData();
                        formData.append('file', files[0]);
                        if (files[0].size > that.imgsize) {
                            Public.ajaxPrompt("图片不能超过100k")
                            return
                        }
                        formData.append('accessToken', Public.getsessionStorage("accessToken"));
                        $.ajax({
                            url: Public.IMGUPLOAD_URL(), //后台文件上传接口
                            type: 'POST',
                            data: formData,
                            async: true,
                            cache: false,
                            contentType: false,
                            processData: false,
                            success: function(res) {

                                $('#healthnews').summernote('insertImage', Public.IMGVIEW_URL() + JSON.parse(res).id, 'img');
                            }
                        });

                    }
                },
                height: 300
            });




        },
        methods: {
            //新增和修改弹框
            addhealthnews(item) {
                    $("#addhealthnews").modal("show");
                    if (item) {
                        this.username = item.infoApplyUserName;
                        this.applytime = item.infoApplyTime;
                        this.healthnewssavedata = item;
                        this.imgurl = item.infoTitlePicture;

                        this.modaltitle = "修改健康资讯";
                        $('#healthnews').summernote('code', item.infoContent);
                        
                    } else {
                        this.username = sessionStorage.getItem('userName');
                        this.modaltitle = "新增健康资讯";
                        this.imgurl = "";
                        this.$broadcast("imgurl", this.imgurl);
                        // $("#imgupload").find('a').addClass('none');
                        this.healthnewssavedata = {
                            "infoTitlePicture": 0,
                            "infoTitle": "",
                            "infoContent": "",
                            "infoType": "",
                        };
                        $('#healthnews').summernote('code', "");
                        var d = new Date(); //获取今天日期
                        var year = d.getFullYear();
                        var month = d.getMonth() + 1;
                        var date = d.getDate();
                        var hour = d.getHours();
                        var minute = d.getMinutes();
                        var second = d.getSeconds();
                        this.applytime = year + "-" + (month < 10 ? '0' + month : month) + "-" + (date < 10 ? '0' + date : date) + "   " + (hour < 10 ? '0' + hour : hour) + ":" + (minute < 10 ? '0' + minute : minute) + ":" + (second < 10 ? '0' + second : second);
                    }
                },
                //新增和修改保存
                addhealthnewssave() {
                    var that = this;
                    // if(healthnewssavedata.infoTitlePicture){
                    //    Public.ajaxPrompt("请上传ppt");
                    //    return
                    // }
                    //图片地址转换成数字ID
                    var imgid = this.healthnewssavedata.infoTitlePicture;
                    if (typeof(this.healthnewssavedata.infoTitlePicture) != "number") {
                        var index = this.healthnewssavedata.infoTitlePicture.indexOf("upload/");
                        this.healthnewssavedata.infoTitlePicture = this.healthnewssavedata.infoTitlePicture.substr(index + 7);
                    }

                    var summernote_con = $('#healthnews').summernote('code');
                    this.healthnewssavedata.infoContent = summernote_con;
                    $.when(Public.commonajax("coms.healthMessageService", "saveHealthMessage", "[" + JSON.stringify(this.healthnewssavedata) + "]"))
                        .done(function(res) {
                            if (res.code == 200) {
                                Public.ajaxPrompt("提交成功");
                                $("#addhealthnews").modal("hide");
                                that.healthnewssavedata.infoTitlePicture = imgid;
                                that.$dispatch('healthnewssave');

                            }

                        })
                },
        },
        events: {

            //上传文件返回的数据
            'serviceIconId' (data) {
                this.healthnewssavedata.infoTitlePicture = data
            },


        },
}
</script>
<style scoped>
</style>
